<?php

namespace app\home\controller;

use think\Db;
use \app\common\logic\SmsLogic;

class Api extends Base {
    public  $send_scene;
    
    public function _initialize() {
        parent::_initialize();
    }
    /*
     * 获取地区
     */
    public function getRegion(){
        $parent_id = I('get.parent_id/d');
        $selected = I('get.selected',0);
        $level = I('get.level',1);
        $data = M('region')->where(array("parent_id"=>$parent_id,'level'=>$level))->select();
        $html = '';
        if($data){
            foreach($data as $h){
            	if($h['id'] == $selected){
            		$html .= "<option value='{$h['id']}' selected>{$h['name']}</option>";
            	}
                $html .= "<option value='{$h['id']}'>{$h['name']}</option>";
            }
        }
        echo $html;
    }

    /**
     * 获取市或者区
     */
    public function getRegionByParentId(){
        $parent_id = input('parent_id');
        $res = array('status' => 0, 'msg' => '获取失败，参数错误', 'result' => '');
        if($parent_id != ''){
            $region_list = Db::name('region')->field('id,name')->where(['parent_id'=>$parent_id])->select();
            $res = array('status' => 1, 'msg' => '获取成功', 'result' => $region_list);
        }
        exit(json_encode($res));
    }
    
    /*
     * 获取商品分类
     */
    public function get_category(){
        $parent_id = I('get.parent_id/d'); // 商品分类 父id
            $list = M('goods_category')->where("parent_id", $parent_id)->select();
        $html = '';
        foreach($list as $k => $v)
            $html .= "<option value='{$v['id']}'>{$v['name']}</option>";        
        exit($html);
    }
    
    
    /**
     * 前端发送短信方法: APP/WAP/PC 共用发送方法
     */
    public function send_validate_code(){
         
        $this->send_scene = C('SEND_SCENE');

        $scene = I('scene');    //发送短信验证码使用场景
        $mobile = I('send');
        if(!check_mobile($mobile)){
            ajaxReturn(array('status'=>-1,'msg'=>'手机号码格式不正确'));
        }
        if($scene == 2){
            $user = Db::name('user')->where(array('phone'=>$mobile))->find();
            if(!$user){
                ajaxReturn(array('status'=>-1,'msg'=>'用户不存在'));
            }
        }elseif($scene == 1){
            $user = Db::name('user')->where(array('phone'=>$mobile))->find();
            if($user){
                ajaxReturn(array('status'=>-1,'msg'=>'用户已注册'));
            }
        }

        $session_id = I('unique_id' , session_id());
        session("scene" , $scene);

        //发送短信验证码
        $res = checkEnableSendSms($scene);
        if($res['status'] != 1){
            ajaxReturn($res);
        }

        //判断是否存在验证码
        $data = M('sms_log')->where(array('mobile'=>$mobile,'session_id'=>$session_id, 'status'=>1))->order('id DESC')->find();
        //获取时间配置
        $sms_time_out = tpCache('sms.sms_time_out');
        $sms_time_out = $sms_time_out ? $sms_time_out : 120;
        //ajaxReturn($sms_time_out);exit;
        //120秒以内不可重复发送
        if($data && time() < ($data['add_time'] + $sms_time_out)){
            $return_arr = array('status'=>-1,'msg'=>$sms_time_out.'秒内不允许重复发送');
            ajaxReturn($return_arr);
        }
        //随机一个验证码
        $code = rand(100000, 999999);

        $params['code'] =$code;

        //发送短信
        $resp = sendSms($scene , $mobile , $params, $session_id);
        $sms_time_over = tpCache('sms.sms_time_over');
        $sms_time_over = $sms_time_over ? $sms_time_over : 180;
        $mu = ($sms_time_over/60).'分钟有效';
        if($resp['status'] == 1){
            //发送成功, 修改发送状态位成功
            M('sms_log')->where(array('mobile'=>$mobile,'code'=>$code,'session_id'=>$session_id , 'status' => 0))->save(array('status' => 1));
            $return_arr = array('status'=>1,'msg'=>'发送成功,请注意查收','time'=>$sms_time_out);
        }else{
            $return_arr = array('status'=>-1,'msg'=>'发送失败('.$resp['msg'].')');
        }
        ajaxReturn($return_arr);
    }
    //登录验证码
    public function l_verify(){
        verify('hy_login');
    }

    public function upload(){
        $func = I('func');
//        return $func;
        $path = I('path','temp');
        $info = array(
            'num'=> I('num/d'),
            'title' => '',
            'upload' =>U('mingteng/Ueditor/imageUp',array('savePath'=>$path,'pictitle'=>'banner','dir'=>'images')),
            'fileList'=>U('mingteng/Uploadify/fileList',array('path'=>$path)),
            'size' => '1M',
            'type' =>'jpg,png,gif,jpeg',
            'input' => I('input'),
            'func' => empty($func) ? 'undefined' : $func,
        );
        $this->assign('info',$info);
        return $this->fetch();
    }

    /*
     删除上传的图片
    */
    public function delupload(){
        $action= isset($_GET['action']) ? $_GET['action'] : 'del';
        $filename= isset($_GET['filename']) ? $_GET['filename'] : I('url');
        $filename= str_replace('../','',$filename);
        $filename= trim($filename,'.');
        $filename= trim($filename,'/');
        if($action=='del' && !empty($filename)){
            $size = getimagesize($filename);
            $filetype = explode('/',$size['mime']);
            if($filetype[0]!='image'){
                return false;
                exit;
            }
            unlink($filename);
            exit;
        }
    }




    public function notice(){
        //13548049118
        (new SmsLogic())->notice('13880449239',date('Y年m月d日'),'9','11:00');
    }


}